﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework
{
    class Program
    {
        static void Main(string[] args)
        {
            string constr = "Server=.;integrated security=SSPI;Initial Catalog=TestDB";
            SqlConnection mysqlCon = new SqlConnection(constr);
            try
            {
                //mysqlCon.Open();
                //string sql = "select * from Student";
                //SqlCommand myCom = new SqlCommand(sql, mysqlCon);
                //myCom.CommandTimeout = 2;
                //Console.WriteLine("创建对象成功");

                //mysqlCon.Open();
                //string sql = "insert Student(Name,Sex,Age)values('李华','男',12)";
                //SqlCommand myCom = new SqlCommand(sql, mysqlCon);
                //myCom.ExecuteNonQuery();
                //Console.WriteLine("插入数据完成");

                mysqlCon.Open();
                string sql = "select * from Student";
                SqlCommand myCom = new SqlCommand(sql, mysqlCon);

                //获取数据
                SqlDataReader myDR;
                myDR = myCom.ExecuteReader();
                if (myDR.HasRows)
                {
                    Console.WriteLine("Student表中存在数据");
                }
                else
                {
                    Console.WriteLine("Student表中不存在数据");
                }

                if (myDR.Read())
                    Console.WriteLine(myDR.GetValue(1));

                object[] myobj = new object[myDR.FieldCount];
                while (myDR.Read())
                {
                    myDR.GetValues(myobj);
                    foreach (object outobj in myobj) Console.Write(outobj + "\t");
                    Console.WriteLine();
                }
                myDR.Close();
                Console.WriteLine(myDR.IsClosed);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            finally
            {
                mysqlCon.Close();
            }



        } 
    }
}
